package cn.tedu._03vehicle.controller;

import cn.tedu._03vehicle.base.response.JsonResult;
import cn.tedu._03vehicle.base.response.StatusCode;
import cn.tedu._03vehicle.mapper.UserMapper;
import cn.tedu._03vehicle.pojo.dto.UserLoginParam;
import cn.tedu._03vehicle.pojo.dto.UserRegDTO;
import cn.tedu._03vehicle.pojo.entity.User;
import cn.tedu._03vehicle.pojo.vo.UserVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.Date;

@Slf4j
@Api(tags = "01.用户模块")
@RestController
@RequestMapping("/v1/user/")
public class UserController {
    @Autowired
    private UserMapper userMapper;

    @ApiOperation("注册功能")
    @ApiOperationSupport(order = 10)
    @PostMapping("reg")
    public JsonResult reg(@RequestBody @Validated UserRegDTO userRegDTO){
        /**
         * 梳理业务逻辑,确定接口;
         * 1.接收客户端传递的数据;
         * 2.校验数据合法性[后期使用验证框架做];
         * 3.验证用户名是否被占用[SELECT ... FROM user WHERE username=?]
         *   3.1 查询结果不为null: 被占用;
         *   3.2 查询结果为null: 可以用[INSERT INTO user... VALUES ...];
         */
        log.debug("userRegDTO = " + userRegDTO);
        String username = userRegDTO.getUsername();
        UserVO userVO = userMapper.selectByUsername(username);
        //判断
        if (userVO != null){//被占用
            return new JsonResult(StatusCode.USERNAME_EXISTS);
        }
        //未被占用
        User user = new User();
        BeanUtils.copyProperties(userRegDTO, user);
        user.setCreateTime(new Date());
        user.setUpdateTime(new Date());
        log.debug("user = " + user);
        userMapper.insert(user);

        return JsonResult.ok();
    }

    /**
     * 2.登录功能
     */
    @PostMapping("login")
    @ApiOperation("登录功能")
    @ApiOperationSupport(order = 20)
    public JsonResult login(@RequestBody UserLoginParam userLoginParam, @ApiIgnore HttpSession session){
        /*
            1.接收数据[用户名、密码];
            2.数据库中查询确认[SELECT ... FROM ... WHERE username=? AND password=?]
            3.判断查询结果是否为null
              3.1 为null: 用户名或密码错误;
              3.2 不为null: 登录成功;
         */
        log.debug("userLoginParam = " + userLoginParam);
        String username = userLoginParam.getUsername();
        String password = userLoginParam.getPassword();
        UserVO userVO = userMapper.selectByUsernameAndPassword(username, password);
        //判断
        if (userVO != null){//登录成功
            //把用户的标识存入服务器端[暂时存储到内存中]
            session.setAttribute("user", userVO);
            return JsonResult.ok(userVO);
        }
        return new JsonResult(StatusCode.USERNAME_PASSWORD_ERROR);
    }

    /**
     * 3.获取当前用户功能
     */
    @GetMapping("currentUser")
    @ApiOperation("获取当前用户")
    @ApiOperationSupport(order = 30)
    public JsonResult currentUser(@ApiIgnore HttpSession session){
        UserVO userVO = (UserVO) session.getAttribute("user");

        //情况1: {"state":2000,"message":"操作成功","data":null}
        //情况2: {"state":2000,"message":"操作成功","data":{"id":22,"username":"xxx"}}
        //直接返回userVO的数据,交由Web前端来判断是否为null值;
        return JsonResult.ok(userVO);
    }

    /**
     * 4.退出登录功能
     * 测试思路：
     * 1.登录成功;
     * 2.获取当前用户[有用户信息];
     * 3.退出登录;
     * 4.获取当前用户[没有用户信息];
     */
    @GetMapping("logout")
    @ApiOperation("退出登录")
    @ApiOperationSupport(order = 40)
    public void logout(@ApiIgnore HttpSession session){
        //删除会话标识
        session.removeAttribute("user");
    }

    @PostMapping("sms")
    public JsonResult sms(String phone){
        //调用云通讯的接口发送短信
        //将验证码存入Redis数据库[10分钟]: {"13603263409":123456}
        return JsonResult.ok();
    }
}





